/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import java.io.*;
import javax.xml.parsers.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class ProcessLogPreferencesSaxParser
    extends DefaultHandler {

  static ProcessLog processLog;
  static String value;
  static String id;
  static String address;
  static ProcessCase curCase;
  static ProcessTaskType curTask;
  static ProcessEventType curEvent;

  static boolean casesStarted = false;
  static boolean caseStarted = false;
  static boolean tasksStarted = false;
  static boolean taskStarted = false;
  static boolean eventsStarted = false;
  static boolean charactersStarted = false;
  static String currentElement;

  static final int ELEMENT_NONE = 0;
  static final int ELEMENT_FROM = 1;
  static final int ELEMENT_TO = 2;
  static final int ELEMENT_CC = 3;
  static final int ELEMENT_BCC = 4;
  static final int ELEMENT_SUBJECT = 5;
  static final int ELEMENT_CASE = 6;
  static final int ELEMENT_TASK = 7;
  static final int ELEMENT_PHASE = 8;

  public static void main(String[] args) {
    if (args.length != 1) {
      System.out.println(
          "Usage: java org.processmining.email.ProcessLogPreferencesSaxParser [URI]");
      System.exit(0);
    }

    String uri = args[0];
    try {
      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
      parserFactory.setValidating(false);
      parserFactory.setNamespaceAware(false);
      ProcessLogPreferencesSaxParser ProcessLogPreferencesSaxParserInstance = new
          ProcessLogPreferencesSaxParser();
      SAXParser parser = parserFactory.newSAXParser();
      parser.parse(uri, ProcessLogPreferencesSaxParserInstance);
    }
    catch (IOException exception) {
      exception.printStackTrace();
    }
    catch (SAXException exception) {
      exception.printStackTrace();
    }
    catch (ParserConfigurationException exception) {
      exception.printStackTrace();
    }
    catch (FactoryConfigurationError exception) {
      exception.printStackTrace();
    }
  }

  public ProcessLogPreferencesSaxParser() {

  }

  public ProcessLogPreferencesSaxParser(ProcessLog log) {
    processLog = log;
  }


  public void setProcessLog(ProcessLog log) {
    processLog = log;
  }

  /**
   * @todo Implement method "characters"
   */
  public void characters(char[] ch, int start, int length) throws SAXException {
    if (!charactersStarted) {
      value = new String(ch, start, length);
    } else {
      value = value + new String(ch, start, length);
    }
    value = value.trim();
//    System.out.println("Value: "+value);
//    System.out.println("Start: "+start);
//    System.out.println("Length: "+length);

//    if((value.length()>0)&&(value.length()-start>length)) {
//      value = value.substring(start, length);
//    }
    charactersStarted = true;
  }

  /**
   * @todo Implement method "endDocument"
   */
  public void endDocument() throws SAXException {
//    throw new java.lang.UnsupportedOperationException(
//        "Method endDocument() not yet implemented.");
  }

  /**
   * @todo Implement method "endElement"
   */
  public void endElement(String url, String localName, String qName) throws
      SAXException {
//    throw new java.lang.UnsupportedOperationException(
//        "Method endElement() not yet implemented.");
      if(localName.equals("Alias")) {
        if(caseStarted) {
          curCase.addAlias(value);
        }
        if(taskStarted) {
          curTask.addAlias(value);
        }
      }

      if(localName.equals("Case")) {
        caseStarted = false;
      }

      if(localName.equals("Task")) {
        taskStarted = false;
      }

  }

  /**
   * @todo Implement method "startDocument"
   */
  public void startDocument() throws SAXException {
//    throw new java.lang.UnsupportedOperationException(
//        "Method startDocument() not yet implemented.");
  }

  /**
   * @todo Implement method "startElement"
   */
  public void startElement(String uri, String localName, String qName,
                           Attributes attributes) throws SAXException {
//    throw new java.lang.UnsupportedOperationException(
//        "Method startElement() not yet implemented.");



    if(localName.equals("Cases")) {
      processLog.setCaseSourceType(Integer.parseInt(attributes.getValue("source")));
      processLog.setPartialCoincidenceCasesAllowed(Boolean.valueOf(attributes.getValue("partial")).booleanValue());
    }

    if(localName.equals("Tasks")) {
      processLog.setTaskSourceType(Integer.parseInt(attributes.getValue("source")));
      processLog.setPartialCoincidenceTasksAllowed(Boolean.valueOf(attributes.getValue("partial")).booleanValue());
    }

    if(localName.equals("Events")) {
      processLog.setEventSourceType(Integer.parseInt(attributes.getValue("source")));
      processLog.setHasToAssignMissingEvents(Boolean.valueOf(attributes.getValue("missing")).booleanValue());
      processLog.setHasToSaveMessageData(Boolean.valueOf(attributes.getValue("saveMessageData")).booleanValue());
      processLog.setIgnoreReplies(Boolean.valueOf(attributes.getValue("replies")).booleanValue());
    }

    if(localName.equals("Case")) {
      caseStarted = true;
      curCase = processLog.addCase(attributes.getValue("name"));
    }

    if(localName.equals("Task")) {
      taskStarted = true;
      curTask = processLog.addTaskType(attributes.getValue("name"));
    }

    if(localName.equals("Event")) {
      processLog.addEventType(attributes.getValue("name"), attributes.getValue("alias"));
    }
    charactersStarted = false;
  }


}
